'use strict'

let { sequelize } = require('../db');
let fs = require('fs');

function searchFiles() {
    let files = fs.readdirSync(__dirname);
    return files.filter(name => {
        return name.endsWith('.js') && name !== 'index.js';
    });
}

function registerModel(files) {
    let obj = {};
    files.forEach(name => {
        let modelNome = name.substring(0, name.length - 3);
        obj[modelNome] = require(__dirname + '/' + name);
    });
    return obj;
}

let files = searchFiles();
let obj = registerModel(files);

obj.sync = async () => {
    return sequelize.sync({ force: true });
}

module.exports = obj;